package 第七周;

/**
 * @author xhw
 */

public class 字符串相乘 {
    public static void main(String[] args) {

        System.out.println(multiply("123", "456"));
    }

    private static String multiply(String number, String number1) {
        if (number.equals("0") || number1.equals("0")) {
            return "0";
        }

        int[] res = new int[number.length() + number1.length()];

        for (int i = number.length() - 1; i >= 0; i--) {
            int n1 = number.charAt(i) - '0';
            for (int j = number1.length() - 1; j >= 0; j--) {
                int n2 = number1.charAt(j) - '0';
                int sum = res[i + j + 1] + n1 * n2;
                res[i + j + 1] = sum % 10;
                res[i + j] += sum / 10;
            }
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < res.length; i++) {
            if (i == 0 && res[i] == 0) {
                continue;
            }
            sb.append(res[i]);
        }
        return sb.toString();
    }
}
